<template>
  <el-card class="m-8">

    <section>

      <section slot="header" class="mt-10 flex items-center">
        <el-button class="mr-4" type="primary" @click="addIndepUser">新增</el-button>
        <!-- <el-button
          icon="el-icon-delete"
          type="danger"
          @click="deBounceWrap(handleDelAll)"
        >批量删除
        </el-button> -->
        <el-popconfirm
              cancel-button-text="不用了"
              confirm-button-text="好的"
              icon="el-icon-info"
              icon-color="red"
              title="确定批量删除吗？"
              @confirm="deBounceWrap(handleDelAll)"
            >
              <el-button slot="reference" type="danger">批量删除</el-button>
            </el-popconfirm>
      </section>

      <el-table :data="tableList" align="center" border
                class="mt-4" stripe @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection"></el-table-column>

        <el-table-column label="序号" prop="id"/>
        <el-table-column label="标签名称" prop="name"/>
        <el-table-column label="关联技能" prop="text">
          <template slot-scope="{row}">
            <el-tag v-for="item in row.ability" color="mr-3" type="primary" :key="item.id">{{ item.name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="created_at"/>
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <el-button
                  icon="el-icon-edit"
                  plain
                  size="small"
                  type="primary"
                  style="margin-right: 10px;"
                  @click="addIndepUser(row)"
              >编辑
            </el-button>
            <el-popconfirm
              cancel-button-text="不用了"
              confirm-button-text="好的"
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="delRow(row)"
            >
              <el-button slot="reference" type="danger">删除</el-button>
            </el-popconfirm>

          </template>
        </el-table-column>
      </el-table>

      <cus-pagination
        :limit.sync="dataForm.per_page"
        :page.sync="dataForm.page"
        :total="total"
        @pagination="initData"
      />

    </section>


    <el-dialog v-dialogDrag :title="!isEdit?'新增':'编辑'" :visible.sync="dialogFormVisible" :close-on-click-modal="false"
               @close="handleCloseDialog('addForm')" width="30%"
    >
      <el-form ref="addForm" :model="dynamicValidateForm" :rules="rules" label-width="150px">

        <el-form-item
          label="标签名称"
          prop="name"
        >
          <el-input
            v-model="dynamicValidateForm.name"
            placeholder="请输入"
            maxlength="4"
            show-word-limit
          ></el-input>
        </el-form-item>

        <el-form-item
          label="关联技能"
          prop="sex"
        >
          <el-select v-model="dynamicValidateForm.game_id" multiple placeholder="请选择">
            <el-option
              v-for="item in skillOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="handleAddUser('addForm')">保存</el-button>
          <el-button type="danger" @click="dialogFormVisible = false">取消</el-button>
        </span>
    </el-dialog>


  </el-card>
</template>

<script>
// import { banStatus, deleteData, getData, postData, putEditData } from '@/api/interaction/sayHello'

import { listData, skillConfigList, skillTagAdd, skillTagDel, skillTagEdit } from '../../api/playManagement/skillTag'

export default {
  data() {
    return {
      selectionList: [],
      rowId: '',
      dialogPoolVisible: false,
      detailFormVisible: false,
      dialogFormVisible: false,
      dynamicValidateForm: {
        game_id: [],
        name: ''
      },
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      skillOptions: [],
      tableList: [],
      dateList: undefined,
      dataForm: {
        page: 1,
        per_page: 10
      },
      meta: {
        page: 1,
        perPage: 10
      },
      total: 1,
      isEdit: false
    }
  },
  created() {
    this.initData()
    this.initSelect()
  },
  methods: {
    async initSelect() {
      const { data } = await skillConfigList({
        page: 1,
        per_page: 999
      })
      this.skillOptions = data.data
    },
    handleDelAll() {
      if (this.selectionList.length) {
        /* 存在勾选数据 */
        const ids = this.selectionList.map(item => item.id)
        skillTagDel(ids)
          .then(() => this.$message.success('删除成功！'))
          .catch(console.error)
          .finally(() => /* 刷新数据 */ {
            this.selectionList = []
            this.initData()
          })
      } /* 不存在勾选数据 */ else {
        this.$message.warning('请勾选要删除的数据！')
      }
    },
    handleSelectionChange(list) {
      this.selectionList = list
    },
    handleCloseDialog(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate() // 清除整个表单的校验
      }
    },

    async initData() {
      try {
        const { data } = await listData(this.dataForm)
        this.tableList = data.data
        this.dataForm.page = data.current_page
        this.total = data.total

      } catch (e) {
        console.error(e)
      }
    },
    async delRow(row) {
      let arr = [row.id]
      skillTagDel(arr).then((response) => {
        if (response.code === 200) {
          this.initData()
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          })
        }
      })
    },

    handleAddUser(val) {
      this.$refs[val].validate(valid => {
        if (valid) {
          let obj = {}
          for (let key in this.dynamicValidateForm) {
            if (this.dynamicValidateForm[key] !== undefined && this.dynamicValidateForm[key] !== '') {
              obj[key] = this.dynamicValidateForm[key]
            }
          }

          if (!this.isEdit) {
            skillTagAdd(obj).then(res => {
              if (res.code === 200) {
                this.initData()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              }
            })
          } else {
            skillTagEdit(obj).then(res => {
              if (res.code === 200) {
                this.initData()
                this.dialogFormVisible = false
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                })
              }
            })
          }

        } else {
          return false
        }
      })
    },
    addIndepUser(row) {
      if (row.id) {
        this.rowId = row.id
        this.isEdit = true
        this.dynamicValidateForm = {
          id: row.id,
          name: row.name,
          game_id: row.game_id.split(',').map(item => parseInt(item))
        }
      } else {
        this.dynamicValidateForm = {
          id: undefined,
          name: '',
          game_id: []
        }
        this.rowId = ''
        this.isEdit = false
      }
      this.dialogFormVisible = true
    },
    handleReset() {
      this.dataForm = {
        page: 1,
        per_page: 10
      }
    }

  }
}
</script>

<style scoped lang="scss">


::v-deep {
  .el-select{
    width: 100%;
  }
  .el-table .cell {
    flex-wrap: wrap;
  }
} 
</style>
